//
//  MovieViewController.m
//  LZMPro
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 LZS. All rights reserved.
//

#import "MovieViewController.h"
#import "BaseTableView.h"
#import "MovieModel.h"
#import "AFNData.h"
#import "MovieDetailViewController.h"
#import "MovieTableViewcell.h"
#import <MBProgressHUD.h>
#import <MJRefresh.h>


@interface MovieViewController ()<UITableViewDataSource,UITableViewDelegate,MBProgressHUDDelegate,DetailVCpopProtocol>

//下一页的url
@property(nonatomic, copy) NSString *nextPageUrl;
@property(nonatomic, assign) BOOL isUpdraw;

//model数组
@property(nonatomic, strong) NSMutableArray *modelArray;
//tableview
@property(nonatomic, strong) BaseTableView *myTableView;

//菊花和上拉下拉刷新
@property(nonatomic, strong)MBProgressHUD *HUD;
@property(nonatomic, strong)UIViewController *popController;

@end


@implementation MovieViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化属性数组
    self.navigationItem.title = @"发现";
    self.navigationController.navigationBar.translucent = NO;
    self.modelArray = [NSMutableArray array];
    
    
    //rightbarbutton
    
//    UIImage *eyeImage = [UIImage imageNamed:@"iconfont-yan.png"];
//    eyeImage = [eyeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:eyeImage style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAction)];
    
    self.myTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, CURRENTWIDTH, CURRENTHEIGHT - 64) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];

    //上拉下拉
    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.nextPageUrl = @"http://baobab.wandoujia.com/api/v1/feed?num=1&udid=5614f5b294c8474a8252daad4f23ea3cbc5401e5&vc=51&vn=1.6.1&deviceModel=PE-TL10&first_channel=eyepetizer_zhihuiyun_market&last_channel=eyepetizer_zhihuiyun_market";
        //下拉清空数组
        self.isUpdraw = NO;
        [self AFNetWorkingData:self.nextPageUrl];
        
    }];
    
    [self.myTableView.header beginRefreshing];

    
    self.myTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSLog(@"上拉");
        self.isUpdraw = YES;
        [self AFNetWorkingData:self.nextPageUrl];
        
    }];
    

//    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    [self.view addSubview:self.HUD];
//    self.HUD.delegate = self;
//    self.HUD.backgroundColor = [UIColor whiteColor];
//    self.HUD.mode = MBProgressHUDModeIndeterminate;
//    self.HUD.labelText = @"加载中...";

}

//右边开眼效果
-(void)rightBarButtonAction{
    
}

-(void)AFNetWorkingData:(NSString *)urlStr{


    [AFNData getAFNDataWithUrl:urlStr success:^(id responseObject) {
        
        if (self.isUpdraw == NO) {
            //如果是上拉刷新
            [self.modelArray removeAllObjects];
        }
        
        NSDictionary *dic1 = responseObject;
        //存储下一页的地址
        self.nextPageUrl = [dic1 objectForKey:@"nextPageUrl"];
        NSArray *array = [dic1 objectForKey:@"dailyList"];
        NSDictionary *resultDic = [array objectAtIndex:0];
        
        NSArray *resultArray = [resultDic objectForKey:@"videoList"];
        for (NSMutableDictionary *dic in resultArray) {
            
            MovieModel *model = [[MovieModel alloc] init];
            [model setVedioModelWithDic:dic];
            [self.modelArray addObject:model];
        }

        [self.myTableView reloadData];
        
        //上拉下拉效果停止
        [self.myTableView.header endRefreshing];
        [self.myTableView.footer endRefreshing];
        //隐藏菊花
//        [self hudWasHidden:self.HUD];
        
    } failure:^(NSError *error) {
        //隐藏菊花
//        [self hudWasHidden:self.HUD];
        
    }];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIndetefier = @"vediocell";
    MovieTableViewcell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetefier];
    if (cell == nil) {
        cell = [[MovieTableViewcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetefier];
    }
    //取消点击cell的效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    MovieModel *model = [self.modelArray objectAtIndex:indexPath.row];
    
    [cell setCellVithModel:model];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 200;
}

//点击进入详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

    
//    self.detailVC = [[MovieDetailViewController alloc] init];
//    self.detailVC.currentRow = indexPath.row;
    
    //获得点击的cell的坐标
    CGRect currentCell = [tableView rectForRowAtIndexPath:indexPath];
    CGRect rect = [tableView convertRect:currentCell toView:[tableView superview]];
    
    MovieDetailViewController *detailVC = [[MovieDetailViewController alloc] init];
    detailVC.interRow = indexPath.row;
    detailVC.currentRow = indexPath.row;
    detailVC.Vrect = rect;
    detailVC.scrollSize = self.myTableView.contentSize.height;
    detailVC.modelArray = [NSMutableArray array];
    detailVC.modelArray = self.modelArray;

    detailVC.currentModel = [self.modelArray objectAtIndex:indexPath.row];
    detailVC.myDelegate = self;
    
    //
    NSInteger first = self.myTableView.contentOffset.y / 200;
    
    float last = (self.myTableView.contentOffset.y + (667 -113) )/ 200;
    
    NSLog(@"first==%ld,last==%.2f",first,last);
    
    if ((last - (NSInteger)last) == 0){
        //如果当前页面完整三个
        last = last -1;
        
    }

    
    detailVC.first = first;
    detailVC.last = last;
    
    [self addChildViewController:detailVC];
    [self.view addSubview:detailVC.view];
    self.popController = detailVC;

}


-(void)changRow:(NSInteger)page
{
    NSInteger first = self.myTableView.contentOffset.y / 200;
    
    float last = (self.myTableView.contentOffset.y + (667 -113) )/ 200;
    
    if ((last - (NSInteger)last) == 0){
        last = last -1;
        
    }
    
    if (page <= (NSInteger)last && page >= first) {
        
        
        
    }else
    {
        if (page * 200 > (self.myTableView.contentSize.height - CURRENTHEIGHT)) {
            NSLog(@"在最后三个");
            self.myTableView.contentOffset = CGPointMake(0, self.myTableView.contentSize.height - CURRENTHEIGHT + 113);
        }else{
            NSLog(@"正常不再最后三个");
            
            self.myTableView.contentOffset = CGPointMake(0, page * 200);
        }
        
    }
    
    

}
-(void)popFromController:(NSInteger)page
{
    
    [self.popController.view removeFromSuperview];
    [self.popController removeFromParentViewController];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
